//
//  NetWorkingTools.swift
//  01-WeiBo
//
//  Created by 李晓东 on 17/6/29.
//  Copyright © 2017年 PeanutXu. All rights reserved.
//

import UIKit
import AFNetworking

class NetWorkingTools: AFHTTPSessionManager{

    static let appKey = "3326606937"
    static let redirectUri = "http://www.baidu.com"
    static let appSecret = "1a621f0d94253f8e04ef1001d15e3818"
    static let userInfoLink = "https://api.weibo.com/2/users/show.json"
    static let homeTimeLine = "https://api.weibo.com/2/statuses/home_timeline.json"
    
//    func shareNetWorkingTools() -> NetWorkingTools {
//        let instance : NetWorkingTools = {
//            let tools = NetWorkingTools()
//            return tools
//        }()
//        
//        return instance
//    }
    
    var tokenDict :[String: Any]?{
        if let token = UserAccountViewModel.shareUserAccount.accessToken {
            var params = ["access_token": token]
            return params
        }
        return nil
    }
    
    
    static let shareNetWorkingTools : NetWorkingTools = {
       let tools = NetWorkingTools(baseURL: nil)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
}

/// MARK 获取
extension NetWorkingTools{
    func loadWeiboInfo(finishCallBack:@escaping (Any?,Error?) -> ()){
        guard let params = tokenDict else { return  }
        get(NetWorkingTools.homeTimeLine, parameters: params, progress: { (pro) in
            
        }, success: { (_, result) in
            finishCallBack(result, nil)
        }) { (_, error) in
            finishCallBack(nil, error)
        }
    }
}

extension NetWorkingTools{
    func oauthURL() -> String {
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(NetWorkingTools.appKey)&redirect_uri=\(NetWorkingTools.redirectUri)"
        return url
        
    }
    
    func loadAccessToken(code: String, finishCallBack:@escaping ((Any?, Error?) -> Void)) -> Void {
        let params = ["client_id":NetWorkingTools.appKey,
                      "client_secret":NetWorkingTools.appSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":NetWorkingTools.redirectUri]
        
        post("https://api.weibo.com/oauth2/access_token", parameters: params, progress: { (pro) in
            
        }, success: { (_, result) in
            finishCallBack(result, nil)
        }) { (_, error) in
            finishCallBack(nil, error)
        }
        
    }
}

extension NetWorkingTools{
    func loadUserInfo(uid:String, finishCallBack:@escaping((Any?, Error?) -> Void)) {
        guard var params = tokenDict else {
            finishCallBack(nil, NSError(domain: "cn.itcast.error", code: -1001, userInfo: ["message": "token 为空"]))
            return
        }
//        let params = ["access_token":accessToken, "uid":uid]
        params["uid"] = uid
        get(NetWorkingTools.userInfoLink, parameters: params, progress: { (pro) in
            print(pro)
        }, success: { (_, result) in
            finishCallBack(result, nil)
        }) { (_, error) in
            finishCallBack(nil, error)
        }
    }
}

//https://api.weibo.com/oauth2/authorize?client_id=&redirect_uri=http://www.baidu.com
